//
//  TweetsCell.m
//  oschina_song
//
//  Created by song on 16/1/28.
//  Copyright © 2016年 song. All rights reserved.
//

#import "TweetsCell.h"
#import "OSCTweets.h"
#import "UIImageView+Util.h"
#import "UIView+Utils.h"

@implementation TweetsCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style  reuseIdentifier:reuseIdentifier];
    if(self)
    {
         self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        [self initSubViews];
        [self initLayout];
    }
    
    return  self;
}


-(void)initSubViews
{
    _protrait =[[UIImageView alloc] init];
    _protrait.contentMode = UIViewContentModeScaleAspectFill;
    [_protrait setCornerRadius:0.5];
    [self.contentView addSubview:_protrait];

    
    _authorLabel = [[UILabel alloc] init];
    _authorLabel.font =[UIFont boldSystemFontOfSize:14];
    [self.contentView addSubview:_authorLabel];
    
    
    _timeLabel =[[UILabel alloc] init];
    _timeLabel.font = [UIFont systemFontOfSize:12];
    _timeLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:_timeLabel];
    
    _appclientLabel =[UILabel new ];
    [self.contentView addSubview:_appclientLabel];
    
    _commentCount = [UILabel new];
    _commentCount.font = [UIFont systemFontOfSize:12];
    _commentCount.textColor = [UIColor grayColor];
    [self.contentView addSubview:_commentCount];
    
    
    _contentTextView = [[UITextView alloc] initWithFrame:CGRectZero];

    [TweetsCell initContentTextView:_contentTextView];
    [self.contentView addSubview:_contentTextView];
    
}

-(void)initLayout
{
    for (UIView *view in self.contentView.subviews) {view.translatesAutoresizingMaskIntoConstraints = NO;}
    
    NSDictionary *views = NSDictionaryOfVariableBindings(_protrait, _authorLabel, _timeLabel, _appclientLabel, _contentTextView, _commentCount);
    
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[_protrait(36)]" options:0 metrics:nil views:views]];
    
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-8-[_protrait(36)]-8-[_authorLabel]-8-|" options:0 metrics:nil views:views]];
    
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-7-[_authorLabel]-5-[_contentTextView]-5-[_timeLabel]-5-|" options:NSLayoutFormatAlignAllLeft metrics:nil views:views]];
    
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_timeLabel]-10-[_commentCount]-5-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_authorLabel  attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_contentTextView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
}




-(void)setContentWithTweet:(OSCTweets *)tweet
{
    [_protrait loadProtrain:tweet.portraitURL];
    [_authorLabel setText:tweet.author];
    
    [_timeLabel setText:@"2016-1-27"];
    [_commentCount setText:@"23"];
    [_contentTextView setText:tweet.body];
}

+(void)initContentTextView:(UITextView *)textview
{
    textview.textContainer.lineBreakMode = NSLineBreakByWordWrapping;
    textview.editable = NO;
    textview.scrollEnabled = NO;
}

@end
